﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Utility
{
    /// <summary>
    /// 对象引用相等比较器
    /// </summary>
    /// <typeparam name="T">泛型参数类型</typeparam>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2009/12/21 12:16    创建
    /// </history>
    public sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T>, IEqualityComparer
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="ReferenceEqualityComparer&lt;T&gt;"/>的新实例
        /// </summary>
        /// <remarks>私有构造函数，防止创建实例</remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 13:03    创建
        /// </history>
        private ReferenceEqualityComparer()
        {
        }

        #endregion

        #region ---IEqualityComparer<T>

        /// <summary>
        /// 确定指定的对象是否相等
        /// </summary>
        /// <param name="x">要比较的第一个类型为<typeparamref name="T"/>的对象</param>
        /// <param name="y">要比较的第二个类型为<typeparamref name="T"/>的对象</param>
        /// <returns>
        /// 如果指定的对象相等，则为<c>True</c>；否则为<c>False</c>。
        /// </returns>
        /// <remarks>比较是否为同一个对象的引用</remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 12:19    创建
        /// </history>
        bool IEqualityComparer<T>.Equals( T x, T y )
        {
            //返回引用相等
            return object.ReferenceEquals( x, y );
        }

        /// <summary>
        /// 返回指定对象的哈希代码
        /// </summary>
        /// <param name="obj">要返回哈希代码的对象</param>
        /// <returns>指定对象的哈希代码。</returns>
        /// <exception cref="System.ArgumentNullException">
        /// 	<paramref name="obj"/>为引用类型并且<paramref name="obj"/>为<c>null</c>引用。
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 12:23    创建
        /// </history>
        int IEqualityComparer<T>.GetHashCode( T obj )
        {
            //判断对象是否为空引用
            if (null != obj)
            {
                //返回哈希代码
                return obj.GetHashCode();
            }

            //默认返回0
            return 0;
        }

        #endregion

        #region ---IEqualityComparer

        /// <summary>
        /// 确定指定的对象是否相等
        /// </summary>
        /// <param name="x">要比较的第一个类型为<see cref="object"/>的对象</param>
        /// <param name="y">要比较的第二个类型为<see cref="object"/>的对象</param>
        /// <returns>
        /// 如果指定的对象相等，则为<c>True</c>；否则为<c>False</c>。
        /// </returns>
        /// <remarks>比较是否为同一个对象的引用</remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 12:19    创建
        /// </history>
        bool IEqualityComparer.Equals( object x, object y )
        {
            //返回是否引用相等
            return object.ReferenceEquals( x, y );
        }

        /// <summary>
        /// 返回指定对象的哈希代码
        /// </summary>
        /// <param name="obj">要返回哈希代码的对象</param>
        /// <returns>指定对象的哈希代码。</returns>
        /// <exception cref="System.ArgumentNullException">
        /// 	<paramref name="obj"/>为引用类型并且<paramref name="obj"/>为<c>null</c>引用。
        /// </exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 12:23    创建
        /// </history>
        int IEqualityComparer.GetHashCode( object obj )
        {
            //判断对象是否为空引用
            if (null != obj)
            {
                //判断类型是否一致
                if (obj is T)
                {
                    //返回哈希嗲吗
                    return obj.GetHashCode();
                }

                //如果类型不兼容，则抛出异常
                throw new ArgumentException( Properties.Resources.MSG_NOTSUPPORTED_INCOMPARABLETYPE );
            }

            //默认返回0
            return 0;
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得引用相等比较器的默认实例
        /// </summary>
        /// <value>引用相等比较器的默认实例</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/12/21 13:02    创建
        /// </history>
        public static ReferenceEqualityComparer<T> Default
        {
            get
            {
                return DEFAULT_INSTANCE;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 默认实例
        /// </summary>
        private static readonly ReferenceEqualityComparer<T> DEFAULT_INSTANCE = new ReferenceEqualityComparer<T>();

        #endregion


    }
}
